﻿using System;
using System.Data;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForPayCompLeteRpt : IEventSubscriber
    {

        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //完工报告
            UFIDA.U9.MO.Complete.CompleteRpt rpt = key.GetEntity() as UFIDA.U9.MO.Complete.CompleteRpt;
            if (rpt == null) return;

            #region 生产订单控制成套领料入库
            if (rpt.SysState == UFSoft.UBF.PL.Engine.ObjectState.Inserted
                || (rpt.SysState == UFSoft.UBF.PL.Engine.ObjectState.Updated && rpt.CompleteQty != rpt.OriginalData.CompleteQty))
            {
                //找到相对应的生产订单
                UFIDA.U9.MO.MO.MO mo = rpt.MO;
                if (mo == null)
                {
                    throw new Exception($"查找生产订单失败!生产订单[{mo.DocNo}]");
                }
                //判断生产订单私有字段4为Yes
                if (mo.DescFlexField.PrivateDescSeg4.Equals("Yes"))
                {
                    //当前订单已发料可成套数量求和
                    string sql = $@"select min(line.IssuedSetableQty) IssuedSetableQty from MO_MO mo
                                    left join MO_MOPickList line on line.MO = mo.ID
                                    where mo.ID = '{mo.ID}' and line.IssueStyle != '4' 
                                    and line.ActualReqQty != 0 and line.ActualReqQty != line.IssuedQty
                                    group by mo.ID";
                    DataSet ds = null;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                    if (ds == null || ds.Tables == null || ds.Tables.Count == 0 || ds.Tables[0].Rows == null || ds.Tables[0].Rows.Count == 0 || ds.Tables[0].Rows[0] == null)
                        return;
                    decimal qty = Decimal.Parse(ds.Tables[0].Rows[0][0].ToString());
                    //当前订单完工数量求和
                    string sql1 = $@"select sum(rpt.CompleteQty) qty from MO_CompleteRpt rpt 
                                    left join MO_MO mo on mo.ID = rpt.MO
                                    where mo.ID ='{mo.ID}'
                                    group by mo.ID";
                    DataSet ds1 = null;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out ds1);

                    decimal qty1 = 0;
                    if (ds1 == null || ds1.Tables == null || ds1.Tables.Count == 0 || ds1.Tables[0].Rows == null || ds1.Tables[0].Rows.Count == 0)
                    {
                        qty1 = 0;
                    }
                    else
                    {
                        qty1 = Decimal.Parse(ds1.Tables[0].Rows[0][0].ToString());
                    }
                    if (qty < (rpt.CompleteQty + qty1))
                        throw new Exception($"完工总数量[{rpt.CompleteQty + qty1}]>最小领料成套数[{qty}]，请进行领料或修改完工数量,生产订单号[{mo.DocNo}]");
                }
            }


            #endregion


        }

    }
}
